/*
 * ModificarCarrera.java
 *
 * Created on Oct 31, 2008, 6:43:30 PM
 */
package wasolicitudesfiec.Administracion.Carreras;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Button;
import com.sun.rave.web.ui.component.Label;
import com.sun.rave.web.ui.component.TextField;
import entidad.Carrera;
import entidad.Parametro;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import wasolicitudesfiec.RequestBean1;
import wasolicitudesfiec.ApplicationBean1;
import wasolicitudesfiec.Menu;
import wasolicitudesfiec.SessionBean1;
import wasolicitudesfiec.Administracion.Carreras.dataproviders.Dp_Carrera;
import wasolicitudesfiec.logicacontrol.Logica;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @author kabad
 */
public class ModificarCarrera extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private Label lblCarrera = new Label();

    public Label getLblCarrera() {
        return lblCarrera;
    }

    public void setLblCarrera(Label l) {
        this.lblCarrera = l;
    }

    // </editor-fold>
    private Dp_Carrera dp_Carrera = new Dp_Carrera();
    private Logica logica = new Logica();
    private int id;
    private TextField txtEspecializacion = new TextField();

    public TextField getTxtEspecializacion() {
        return txtEspecializacion;
    }

    public void setTxtEspecializacion(TextField tf) {
        this.txtEspecializacion = tf;
    }
    private Button btnModificar = new Button();

    public Button getBtnModificar() {
        return btnModificar;
    }

    public void setBtnModificar(Button b) {
        this.btnModificar = b;
    }

    public ModificarCarrera() {
    }

    @Override
    public void init() {
        FacesMessage message;
        // Perform initializations inherited from our superclass
        try {
            super.init();
            // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
            // Initialize automatically managed components
            // *Note* - this logic should NOT be modified
            try {
                _init();
            } catch (Exception e) {
                log("ModificarCarrera Initialization Failure", e);
                throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
            }

            // </editor-fold>
            logica = new Logica();
            String accion = (String) this.getExternalContext().getRequestParameterMap().get("accion");
              Vector<Parametro> listaRg_Parametro = null;
                listaRg_Parametro = (Vector<Parametro>) this.getExternalContext().getSessionMap().get("lista");            
            if ((accion == null)) {
                if (!this.isPostBack()) {
                    String idCarrera = (String) this.getExternalContext().getRequestParameterMap().get("id");
                    if (idCarrera != null) {
                        this.getExternalContext().getSessionMap().put("id", idCarrera);
                    }
                    Parametro carrera = logica.leerParametro(getId());
                    if (carrera == null) {
                        throw new Exception();
                    //va al catch pero igual sigue la ejecucion ejemplo de modificar, habria que detener el rende o algo asi
                    }
                    this.getLblCarrera().setText(carrera.getNombre());
                    listaRg_Parametro=carrera.getListaParametro();
                    this.getExternalContext().getSessionMap().put("lista", listaRg_Parametro);
                    this.getMenu().setTitulo("Administracion - > Carreras - > Modificar carrera");
                }
            }
            if ((accion != null) && accion.equals("eliminar")) {
                String idCarrera = (String) this.getExternalContext().getRequestParameterMap().get("id");
              listaRg_Parametro = (Vector<Parametro>) this.getExternalContext().getSessionMap().get("lista");
                if (idCarrera != null) {
                    this.getExternalContext().getSessionMap().put("id", idCarrera);
                    int id = Integer.parseInt(idCarrera);
                    for (Parametro parametro : listaRg_Parametro) {
                        if (parametro.getIdParametro() == id) {
                            listaRg_Parametro.remove(parametro);
                            break;
                        }
                    }
                    this.getExternalContext().getSessionMap().put("lista", listaRg_Parametro);
                }        
            }
               this.getDp_Carrera().setListaRg_Parametro(listaRg_Parametro);


        } catch (Exception ex) {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ocurrió un error no esperado, comuníquese con el administrador del sistema", ex.getLocalizedMessage());
            getFacesContext().addMessage(null, message);
            Logger.getLogger(ModificarCarrera.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Menu getMenu() {
        return (Menu) getBean("SolicitudesFIEC$Menu");
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    public String hyperlink1_action() {
        // TODO: Replace with your code
        return null;
    }

    public String btnAgregar_action() {
        Parametro especializacion = new Parametro();
        FacesMessage message;
        try {
            String nombre = (String) getTxtEspecializacion().getText();
            if ((nombre == null) || (nombre.trim().equals(""))) {
                message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ingrese un nombre para la especialización", "");
                getFacesContext().addMessage(null, message);
                return null;
            }
            Vector<Parametro> listaRg_Parametro = (Vector<Parametro>) this.getExternalContext().getSessionMap().get("lista");
            for (Parametro parametro : listaRg_Parametro) {
                if (this.getTxtEspecializacion().getText().equals(parametro.getNombre())) {
                    message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "La especialización específica ya existe.", "");
                    getFacesContext().addMessage(null, message);
                    return null;
                }
            }
            especializacion.setNombre((String) this.getTxtEspecializacion().getText());
            especializacion.setIdParametro(listaRg_Parametro.size());
            especializacion.setFkIdCategoria(2);
            listaRg_Parametro.add(especializacion);
            this.getExternalContext().getSessionMap().put("lista", listaRg_Parametro);
            this.getTxtEspecializacion().setText("");
            this.getDp_Carrera().setListaRg_Parametro(listaRg_Parametro);
        } catch (Exception ex) {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ocurrió un error no esperado, comuníquese con el administrador del sistema.", ex.getLocalizedMessage());
            getFacesContext().addMessage(null, message);
            Logger.getLogger(NuevaCarrera.class.getName()).log(Level.SEVERE, null, ex);

        }
        return null;
    }

    public Dp_Carrera getDp_Carrera() {
        return dp_Carrera;
    }

    public void setDp_Carrera(Dp_Carrera dp_Carrera) {
        this.dp_Carrera = dp_Carrera;
    }

    public int getId() {
        String idRol;
        idRol = (String) this.getExternalContext().getSessionMap().get("id");

        return new Integer(idRol);
    }

    public void setId(int id) {
        this.id = id;
    }

    public String btnModificar_action() {
        FacesMessage message;
        Parametro carrera = new Parametro();
        try {
            Vector<Parametro> listaRg_Especializacion = (Vector<Parametro>) this.getExternalContext().getSessionMap().get("lista");
            carrera.setIdParametro(this.getId());
            carrera.setListaParametro(listaRg_Especializacion);
            carrera.setFkIdCategoria(1);
            carrera.setNombre((String) this.getLblCarrera().getText());
            logica.modificarParametro(carrera);
            return "carreras";

        } catch (Exception ex) {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ocurrió un error no esperado, comuníquese con el administrador del sistema.", ex.getLocalizedMessage());
            getFacesContext().addMessage(null, message);
            Logger.getLogger(NuevaCarrera.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}

